<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  
</head>
 <body>
    <button id="btn">提交</button>
    <script>
        
        const btn = document.getElementById("btn")
        const handle = ()=>{
            console.log("向后端发请求");
        }
        btn.addEventListener("click",debounce(handle, 1000))            //绑定的是函数的执行结果

        function debounce(handle,wait){
            let timer = null
            return function (){
            clearTimeout(timer)
          
             timer = setTimeout(()=>{
                handle()
            },wait)
        }
    }
    </script>
</body> 
<!-- <body>
    <button id="btn">提交</button>
  
    <script>
      const btn = document.getElementById("btn");
      
      function handle() {
        console.log('向后端发请求');
        // console.log(this); // 这里的this将指向btn元素
      }
      
      btn.addEventListener("click", debounce(handle, 1000))
  
      function debounce(fn, wait) {     // 防抖
        let timer = null
        return function () {
          if (timer) clearTimeout(timer)   // 如果定时器没执行完就清除原来的重新开始计时
          timer = setTimeout(() => {
            fn()   // 函数handle执行  为为了不改变this指向 -> fn.call(this)
          }, wait)
        }
      }
    </script>
  </body>
</html> -->